package com.mco.controller;

import cn.hutool.core.util.IdUtil;
import com.google.common.primitives.Doubles;
import com.mco.service.RedPackageService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Arrays;

/**
 * @author ：liao.wei
 * @date ：2023/10/7 16:57
 * @package : com.mco.controller
 */
@RestController
@RequestMapping
@RequiredArgsConstructor
public class RedPackageController {
	
	private final RedPackageService service;
	
	@GetMapping("send")
	public String sendPackage(Double totalMoney, int redPackageNumber) {
		String key = IdUtil.simpleUUID();
		BigDecimal[] splitRedPackage = service.sendPackage(key, totalMoney, redPackageNumber);
		
		return key + "\t" + Doubles.asList(Arrays.stream(splitRedPackage).mapToDouble(BigDecimal::doubleValue).toArray());
	}
	
	/**
	 * 抢红包
	 *
	 * @param redPackageKey 红包key
	 * @param userId        用户
	 * @return 抢红包
	 */
	@GetMapping("rob")
	public String robPackage(String redPackageKey, String userId) {
		return service.robPackage(redPackageKey, userId);
	}
}
